package jiang.ThreadPool;

import java.util.concurrent.*;

/**
 * @ClassName TestFixedThreadPool
 * @Description: 创建一个指定大小的线程池
 * @Author jiangshitao
 * @Date 2023/1/28
 */
public class TestFixedThreadPool {

    /**
     * 创建固定线程数量的线程池测试
     * 创建一个固定大小的线程池，该方法可指定线程池的固定大小，对于超出的线程会在LinkedBlockingQueue队列中等待
     * 核心线程数可以指定，线程空闲时间为0
     */
    public void fixedThreadPoolTest() {
        ExecutorService executorService = new ThreadPoolExecutor(
                5, 5,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                new TestThreadPoolFactory("fixedThreadPool"));

        for (int i = 0; i < 10; i++) {
            executorService.submit(() -> {
                        System.out.println(Thread.currentThread().getName());
                    }
            );
        }
    }

    public static void main(String[] args) {
        TestFixedThreadPool testFixedThreadPool = new TestFixedThreadPool();
        long startTime = System.currentTimeMillis();
        testFixedThreadPool.fixedThreadPoolTest();
        long endTime = System.currentTimeMillis();
        System.out.println("共耗时：" + (endTime-startTime) + "ms");
    }
}
